package com.example.administrator.exkotlin.ui.list

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import com.example.administrator.exkotlin.R
import kotlinx.android.synthetic.main.activity_recycle.*

/**
 * recycleView例子
 * */
class RecycleActivity : AppCompatActivity() {
    val listDatas: MutableList<String> = mutableListOf<String>()
    val mRecycleDemoAdapter: RecycleDemoAdapter = RecycleDemoAdapter(listDatas)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycle)
        initDatas()
    }

    private fun initDatas() {
        btn_vertical.setOnClickListener {
            RecycleViewLoad(1)
        }
        btn_horizontal.setOnClickListener {
            RecycleViewLoad(2)
        }
        btn_grild.setOnClickListener {
            RecycleViewLoad(3)
        }
        btn_flow.setOnClickListener {
            RecycleViewLoad(4)
        }

    }

    fun loadDatas(isFlow: Boolean) {
        if (!isFlow && listDatas.size != 1000) {
            listDatas.clear()
            for (cout in 0 until 1000) {
                listDatas.add("" + cout)
            }
        } else if (isFlow) {
            val newDatas = mutableListOf<String>()
            var data: String
            for (cout in 0 until 1001) {
                data = "" + cout
                if (cout % 5 == 0) {
                    data = data + "流式布局010101010101"
                }
                newDatas.add(data)
            }
            listDatas.clear()
            listDatas.addAll(newDatas)
        }

    }


    fun RecycleViewLoad(modle: Int) {

        when (modle) {
            //垂直布局
            1 -> {
                loadDatas(false)
                val linearLayoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
                rv_list.layoutManager = linearLayoutManager
            }
            //横向布局
            2 -> {
                loadDatas(false)
                val linearLayoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
                linearLayoutManager.orientation = androidx.recyclerview.widget.LinearLayoutManager.HORIZONTAL
                rv_list.layoutManager = linearLayoutManager
            }
            //网格布局
            3 -> {
                loadDatas(false)
                val layoutManager = androidx.recyclerview.widget.GridLayoutManager(this, 3)
                rv_list.layoutManager = layoutManager
            }

            //瀑布流
            4 -> {
                loadDatas(true)
                val layoutManager = androidx.recyclerview.widget.StaggeredGridLayoutManager(5, androidx.recyclerview.widget.StaggeredGridLayoutManager.VERTICAL)
                rv_list.layoutManager = layoutManager
            }
            else -> {
                val linearLayoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
                rv_list.layoutManager = linearLayoutManager
            }
        }
        mRecycleDemoAdapter.datas = listDatas
        rv_list.adapter = mRecycleDemoAdapter
        mRecycleDemoAdapter.notifyDataSetChanged()
    }

    class RecycleDemoAdapter(var datas: List<String>) : androidx.recyclerview.widget.RecyclerView.Adapter<RecycleDemoAdapter.ViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val view = LayoutInflater.from(parent?.context).inflate(R.layout.recycle_view_item, parent, false)
            val viewHolder = ViewHolder(view)
            viewHolder.item_view?.setOnClickListener { Toast.makeText(parent?.context, "点击了${viewHolder.item_view.text}", Toast.LENGTH_SHORT).show() }
            return viewHolder
        }

        override fun getItemCount(): Int {
            return datas.size
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val data = datas[position]
            holder?.item_view?.text = data
        }


        inner class ViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {
            val item_view = itemView?.findViewById<TextView>(R.id.tv_item)
        }

    }


}
